package connector;


import java.util.ArrayList;

import GUI.HoldemPanel;
import GUI.Player;
import GUI.PokerTable;

public class Controler extends Thread{
	private PokerTable players;
	private int currentBid;
	private HoldemPanel gamePanel;
	public final static String KICK_PLAYER_OUT = "KICK_OUT";
	public final static String NEW_TABLE = "CREATE_NEW";
	public static final String LEAVE_TABLE = "LEAVE_TABLE";
	public static int playerIndex = 0;
	
	public Controler(HoldemPanel gamePanel, int current){
		/*InetAddress localhost = null;
		try {
			localhost = InetAddress.getLocalHost();
		} catch (UnknownHostException e) {
			
		}
		String localName = localhost.getHostName();*/
		this.players = gamePanel.getActivePlayers();
		this.gamePanel = gamePanel;
		this.currentBid = current;
	}
	public void removePlayer(Player player){
		players.remove(player);
		player.leaveGame();
	}
	@Override
	synchronized
	public void run() {
		Player player;
		int newBid;
		for(int round=0; round<4; round++){
			ArrayList<Player> currentPlayers = players;
			for(int i = 0; i < currentPlayers.size(); i++){
				player = currentPlayers.get(i);
				if(player.notifyMinimum(currentBid)){
					player.takeTurn(!player.isOwner());
					try {
						this.wait(1000);
					} catch (InterruptedException e) {
					}
					newBid = player.getNewBid();
					player.releaseTurn();	
					if(currentBid < newBid){
						currentBid = newBid;
						if(i>0)i = -1;
					}
				}else{
					players.remove(player);
				}
				gamePanel.repaint();
			}
			Thread.yield();
		}
	}

}
